package com.ziq.zcamp.community

import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.GsonUtils
import com.ziq.base.extension.launchWithTryAsyncGlobal
import com.ziq.zcamp.api.request.post.PostSubjectContentListParam
import com.ziq.zcamp.api.respond.post.PostSubjectContentListRespond
import com.ziq.zcamp.api.service.PostingService
import com.ziq.zcamp.api.service.request
import com.ziq.zcamp.bean.post.ImageInfo
import com.ziq.zcamp.bean.post.PostingDetail
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.view.StatusView
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

@HiltViewModel
class SubjectContentListFragmentViewModel @Inject constructor() : ViewModel() {

    @Inject
    lateinit var mPostingService: PostingService

    val statusShow = MutableLiveData<Int>()
    val listRequestFinished = MutableLiveData<Boolean>()
    val listHasMore = MutableLiveData<Boolean>()
    val list = MutableLiveData<MutableList<PostingDetail>>()

    var subjectId = 0
    var page = 1
    val pageSize = 20

    fun onRefresh(showProgress: Boolean = false){
        if(showProgress){
            statusShow.postValue(StatusView.LOADING)
        }
        page = 1
        requestList()
    }

    fun onLoadMore(){
        requestList()
    }

    fun requestList(
        onSuccess: ((result: PostSubjectContentListRespond) -> Unit)? = {
            if (it.data != null){
                val tempList = mutableListOf<PostingDetail>()
                if(page != 1 && list.value != null){
                    tempList.addAll(list.value!!)
                }
                if(it.data?.list?.isNotEmpty() == true){
                    tempList.addAll(it.data?.list!!)
                }
                list.postValue(tempList)

                page++

                if(it.data?.has_more == true){
                    listHasMore.postValue(true)
                } else {
                    listHasMore.postValue(false)
                }

                if(tempList.isNotEmpty()){
                    statusShow.postValue(StatusView.IDLE)
                }else {
                    statusShow.postValue(StatusView.NO_DATA)
                }
            } else {
                if(page == 1){
                    statusShow.postValue(StatusView.NET_ERROR)
                } else {
                    statusShow.postValue(StatusView.IDLE)
                }
                listHasMore.postValue(false)
            }
            listRequestFinished.postValue(true)
        },
        onFail: ((msg: String) -> Unit)? = {
            Toast.makeText(GlobalContextManager.appContext!!, it, Toast.LENGTH_SHORT).show()
            statusShow.postValue(StatusView.NET_ERROR)
            listRequestFinished.postValue(true)
        }) {

        launchWithTryAsyncGlobal(whenTry = {
            val respond = mPostingService.postSubjectContentList(PostSubjectContentListParam().apply {
                this.subject_id = this@SubjectContentListFragmentViewModel.subjectId
                this.page = this@SubjectContentListFragmentViewModel.page
                this.size = pageSize
            }.queryMap).request()

            if(respond.data?.list?.isNotEmpty() == true){
                for(item in respond.data?.list!!){
                    item.parseData()
                }
            }

            respond?.let {
                onSuccess?.invoke(it)
            }
        }, whenCatch = {
            withContext(Dispatchers.Main) {
                onFail?.invoke("$it")
            }
        })
    }



}